const Router = require('koa-router');
const server = new Router();
const sqldata = require('./sqldata');

server.get('/foster', async (ctx)=>{
    //ctx.body = "服务-首页-寄养";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from foster`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
server.get('/record1', async (ctx)=>{
    //ctx.body = "服务-查看记录-寄养";
    let mydata = await new Promise((resolve, reject)=>{
        const val= ctx.request.query;
        let sqlLang = `select * from foster where Uid = '${val.Uid}'`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
server.get('/record1/del', async (ctx)=>{
    //ctx.body = "服务-查看记录-寄养";
    let mydata = await new Promise((resolve, reject)=>{
        const val= ctx.request.query;
        let sqlLang = `DELETE FROM foster
        WHERE Fid=${val.Fid};`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

server.get('/content1', async (ctx)=>{
    //ctx.body = "服务-详情页-寄养";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from foster where Fid='${ctx.request.query.Fid}'`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
server.get('/adopt', async (ctx)=>{
    //ctx.body = "服务-首页-领养";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from adopt`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

server.get('/record2', async (ctx)=>{
    //ctx.body = "服务-查看记录-领养";
    let mydata = await new Promise((resolve, reject)=>{
        const val= ctx.request.query;
        let sqlLang = `select * from adopt where Uid = '${val.Uid}'`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
server.get('/record2/del', async (ctx)=>{
    //ctx.body = "服务-查看记录-寄养";
    let mydata = await new Promise((resolve, reject)=>{
        const val= ctx.request.query;
        let sqlLang = `DELETE FROM adopt
        WHERE Aid=${val.Aid};`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

server.get('/content2', async (ctx)=>{
    //ctx.body = "服务-详情页-领养";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from adopt where Aid='${ctx.request.query.Aid}'`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

server.get('/question', async (ctx)=>{
    //ctx.body = "服务-答题页";
    let mydata = await new Promise((resolve, reject)=>{
        let sqlLang = `select * from qusetion`;
        return sqldata(sqlLang, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})

module.exports = server;